from .exchange import Exchange, ExchangeStatus
from .order_book import OrderBook
from .order_book_sz import OrderBookSZ
from .stock_order import StockOrder

class ExchangeSZ(Exchange):
    def __init__(self):
        super().__init__()

    # 下单
    def place_order(self, order: StockOrder):
        order_symbol: str = order.get_stock_symbol()
        if order_symbol not in self.__orderbooks:
            self.__orderbooks[order_symbol] = OrderBookSZ()
        order_book: OrderBookSZ = self.__orderbooks[order_symbol]
        # TODO: 设置orderbook的前一天收盘价
        # 竞价
        if self.__status == ExchangeStatus.PRE_OPEN_AUCTION or self.__status == ExchangeStatus.PRE_CLOSE_AUCTION:
            order_book.auction()
        # 在集合竞价阶段，直接添加订单到订单簿，不进行撮合
        elif (self.__status == ExchangeStatus.PRE_OPEN_CALL_AUCTIONING or 
              self.__status == ExchangeStatus.PRE_CLOSE_CALL_AUCTIONING):
            order_book.add_to_book(order)
        # 添加订单并尝试撮合
        elif self.__status == ExchangeStatus.CONTINUOUS_AUCTION:
            self.__orderbooks[order_symbol].add_order_and_match(order)